Xbasic

a5_json_extractValues Function

Syntax

C Result = a5_json_extractValues(txt as C)

Arguments

txtCharacter

JSON expression to parse.

Description

Extracts name/value pairs from a JSON string.

Discussion

The a5_json_extractValues function extracts data from a JSON string and returns a CR-LF delimited list of name value pairs separated by a colon.

This example demonstrates parsing a relatively simple JSON string with two small twists: it contains a quoted string, and there is a comma inside the quoted string.

? a5_json_extractValues("{name:Fred, city: Boston, address: \"123 Main St, Suite 23\", state: MA}")
= name:Fred
city: Boston
address: 123 Main St, Suite 23
state: MA

This real-life example parses a JSON string returned by the Twitter API.

c = <<%txt%
#{"completed_in":0.014,"max_id":157517767036248066,"max_id_str":"157517767036248066",
"next_page":"?page=2&max_id=157517767036248066&q=Red%20Sox&rpp=2","page":1,"query":"Red+Sox",
"refresh_url":"?since_id=157517767036248066&q=Red%20Sox",
"results":[{"created_at":"Thu, 12 Jan 2012 17:42:16 +0000",
"from_user":"Liliancje","from_user_id":453416927,"from_user_id_str":"453416927",
"from_user_name":"Lilian Clum","geo":null,"id":157517767036248066,
"id_str":"157517767036248066","iso_language_code":"en","metadata":{"result_type":"recent"},
"profile_image_url":"http://a0.twimg.com/profile_images/1730506616/rnhizx453h_135249004-2_normal.jpg",
"profile_image_url_https":"https://si0.twimg.com/profile_images/1730506616/rnhizx453h_135249004-2_normal.jpg",
"source":"<a href="http://twitterfeed.com" rel="nofollow">twitterfeed</a>",
"text":"JASON VARITEK Boston Red Sox Majestic AUTHENTIC Home Baseball Jersey, 44/LARGE: Same Style Authentic On-Field Je... http://t.co/MR8spX5g",
"to_user":null,"to_user_id":null,"to_user_id_str":null,"to_user_name":null},
{"created_at":"Thu, 12 Jan 2012 17:41:56 +0000",
"from_user":"BR_BOSRedSox","from_user_id":15908353,"from_user_id_str":"15908353",
"from_user_name":"Boston Red Sox","geo":null,"id":157517679777951744,
"id_str":"157517679777951744","iso_language_code":"en","metadata":{"result_type":"recent"},
"profile_image_url":"http://a3.twimg.com/profile_images/1157609127/BR_LOGO_normal.jpg",
"profile_image_url_https":"https://si0.twimg.com/profile_images/1157609127/BR_LOGO_normal.jpg",
"source":"<a href="http://twitterfeed.com" rel="nofollow">twitterfeed</a>",
"text":"http://t.co/B4SxnH3d - Boston Red Sox Headlines - Red Sox Mailbag: Boston In No Rush to Sign Jacoby Ellsbury to L... http://t.co/1oWDduRM",
"to_user":null,"to_user_id":null,"to_user_id_str":null,"to_user_name":null}],
"results_per_page":2,"since_id":0,"since_id_str":"0"}
%txt%

? a5_json_extractValues(c)
= completed_in:0.014
max_id:157517767036248066
max_id_str:157517767036248066
next_page:?page=2&max_id=157517767036248066&q=Red%20Sox&rpp=2
page:1
query:Red+Sox
refresh_url:?since_id=157517767036248066&q=Red%20Sox
results:created_at:Thu, 12 Jan 2012 17:42:16 +0000
from_user:Liliancje
from_user_id:453416927
from_user_id_str:453416927
from_user_name:Lilian Clum
geo:null
id:157517767036248066
id_str:157517767036248066
iso_language_code:en
metadata:result_type:recent
profile_image_url:http://a0.twimg.com/profile_images/1730506616/rnhizx453h_135249004-2_normal.jpg
profile_image_url_https:https://si0.twimg.com/profile_images/1730506616/rnhizx453h_135249004-2_normal.jpg
source:<a href=http://twitterfeed.com rel=nofollow>twitterfeed</a>
text:JASON VARITEK Boston Red Sox Majestic AUTHENTIC Home Baseball Jersey, 44/LARGE: Same Style Authentic On-Field Je... http://t.co/MR8spX5g
to_user:null
to_user_id:null
to_user_id_str:null
to_user_name:null
created_at:Thu, 12 Jan 2012 17:41:56 +0000
from_user:BR_BOSRedSox
from_user_id:15908353
from_user_id_str:15908353
from_user_name:Boston Red Sox
geo:null
id:157517679777951744
id_str:157517679777951744
iso_language_code:en
metadata:result_type:recent
profile_image_url:http://a3.twimg.com/profile_images/1157609127/BR_LOGO_normal.jpg
profile_image_url_https:https://si0.twimg.com/profile_images/1157609127/BR_LOGO_normal.jpg
source:<a href=http://twitterfeed.com rel=nofollow>twitterfeed</a>
text:http://t.co/B4SxnH3d - Boston Red Sox Headlines - Red Sox Mailbag: Boston In No Rush to Sign Jacoby Ellsbury to L... http://t.co/1oWDduRM
to_user:null
to_user_id:null
to_user_id_str:null
to_user_name:null
results_per_page:2
since_id:0
since_id_str:0

See Also